/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.unal.bioingenium.kbmed.util.io;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/*
 *
 * */

/**
 *@author Alejandro Riveros Cruz Riveros Cruz
 *@version FileManager.java,v 1 2008/20/03
 * */
public class FileManager {

    /**
     * @param:
     * @return:
     */
    public FileManager() {
    }

    /**
     * @param:
     * @return:
     */
    private static void recursivelyFileList(List<String> fileList, File filePath, String absolutePath) {
        if (!isSymbolicLink(filePath)) {
            if (filePath.isDirectory()) {
                File[] filePathList = filePath.listFiles();
                for (int i = 0; i < filePathList.length; i++) {
                    recursivelyFileList(fileList, filePathList[i], absolutePath);
                }
            } else {
                fileList.add(filePath.getAbsolutePath().substring(absolutePath.length(), filePath.getAbsolutePath().length()));
            }
        }
    }

    /**
     * @param:
     * @return:
     */
    public static List<String> getRecursivelyFileList(String path) throws IOException {
        File filePath = new File(path);
        if (filePath.exists()) {
            List<String> fileList = new ArrayList<String>();
            recursivelyFileList(fileList, filePath, path);
            return fileList;
        } else {
            throw new IOException("The path don't exist.");
        }
    }

    /**
     * 
     * @param file
     * @return
     */
    public static boolean isSymbolicLink(File file) {
        try {
            return !file.getCanonicalPath().toLowerCase().equalsIgnoreCase(file.getAbsolutePath().toLowerCase());
        } catch (IOException ex) {
            System.err.println(ex);
            return true;
        }
    }

    /**
     * 
     * @param inPath
     * @return
     */
    public static String toStandardPath(String inPath) {
        if (!inPath.endsWith(File.separator)) {
            return inPath + File.separator;
        } else {
            return inPath;
        }
    }

    public static void verifyAndCreatePath(String path) {
        String[] splitPath = path.split(File.separator);
        File file;
        String currentDir = "";
        for (String dir : splitPath) {
            currentDir += dir + File.separator;
            file = new File(currentDir);
            if (!file.exists()) {
                file.mkdir();
            }
        }
    }

    public static String getStandardName(String name) {
        return name.toLowerCase().replace(File.separator, "-").replace(File.pathSeparator, "-").replace(" ", "_");
    }
}
